package com.base.api.emr.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.base.api.bds.BdsClinicService;
import com.base.api.emr.CpoeApplyService;
import com.base.dao.emr.*;
import com.base.utils.TreeUtil;
import com.base.vo.emr.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CacheConfig(cacheNames = {"emr"} )
@Service
public class CpoeApplyServiceImpl implements CpoeApplyService {

    @Autowired
    private CpoeApplyItemClassMapper cpoeApplyItemClassMapper;

    @Autowired
    private CpoeAppCaRelVstMapper cpoeAppCaRelVstMapper;

    @Autowired
    private CpoeDictApplyNewMapper applyNewMapper;

    @Autowired
    private CpoeDictApplyInspRelNewMapper dictApplyInspRelNewMapper;

    @Autowired
    private CpoeBasicDictInspecSampleMapper inspecSampleMapper;


//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    BdsClinicService bdsClinicService;


    /**
     * 查询所有的申请单分类
     *
     * @return
     */

    @Cacheable
    @Override
    public String selectAllApplyItemClass() {
        Map<String,Object> map =new HashMap<>();
        map.put("code","0");
        List<CpoeApplyItemClass> list = cpoeApplyItemClassMapper.selectAll();
        if (!list.isEmpty()) {
            JSONArray jsonArray = TreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(list)), "id", "parentId", "children");
/*            List<Map<String, Object>> list1 = new ArrayList<>();
            try {
                for (CpoeApplyItemClass itemClass : list) {
                    Map<String, Object> map = RecordtoTreeJson.getObjectToMap(itemClass);
                    list1.add(map);
                }
                List<Map<String, Object>> mapList = RecordtoTreeJson.recursion(null, list1);
                return JSON.toJSONString(mapList);
            } catch (Exception e) {
                e.printStackTrace();
            }*/
            return jsonArray.toJSONString();
        }
        return JSON.toJSONString(map);
    }

    /**
     * 根据申请单分类返回对应的申请单列表
     *
     * @param applyItemClass
     * @return
     */
    @Override
    public String selectApplyItemByExecOrgCodeAndCatalogId(CpoeApplyItemClass applyItemClass) {
        Map<String, Object> map = new HashMap<>();
        List<CpoeDictApplyCatalogNew> list = applyNewMapper.selectByApplyItemClass(applyItemClass);
        map.put("code", "0");
        map.put("data", list);
        return JSON.toJSONString(map);
    }

    /**
     * 根据申请单主键查询对应的诊疗项目
     *
     * @param applyId
     * @return
     */
    @Override
    public String selectApplyInsp(Integer applyId) {
        Map<String, Object> map = new HashMap<>();
        List<CpoeDictApplyInspRelNew> list = dictApplyInspRelNewMapper.selectByApplyId(applyId);
        map.put("code", "0");
        map.put("data", list);
        return JSON.toJSONString(map);
    }

    /**
     * 根据检验项目主键查询标本信息
     *
     * @param inspId
     * @return
     */
    @Override
    public String selectApplyInspSampleByInspId(Integer inspId) {
        Map<String, Object> map = new HashMap<>();
        List<CpoeBasicDictInspecSample> list = new ArrayList<>();
        if (inspId != null) {
            list = inspecSampleMapper.selectByInspId(inspId);
        }
        map.put("code", "0");
        map.put("data", list);
        return JSON.toJSONString(map);
    }

    /**
     * 修改检验申请单对照数据
     *
     * @param dictApplyInspRelNew
     * @return
     */
    @Override
    public String uptApplyInspRelNew(CpoeDictApplyInspRelNew dictApplyInspRelNew) {
        Map<String, Object> map = new HashMap<>();
        if (dictApplyInspRelNew != null) {
            dictApplyInspRelNewMapper.update(dictApplyInspRelNew);
        }
        map.put("code", "0");
        return JSON.toJSONString(map);
    }

    /**
     * 修改检验申请标本
     *
     * @param inspecSample
     * @return
     */

    @Override
    public String uptApplyInspSample(CpoeBasicDictInspecSample inspecSample) {
        Map<String, Object> map = new HashMap<>();
        if (inspecSample != null) {
            inspecSampleMapper.update(inspecSample);
        }
        map.put("code", "0");
        return JSON.toJSONString(map);
    }

    /**
     * 查询检验标本字典
     *
     * @return
     */
    @Override
    public String getAllSampleDetail() {
        return bdsClinicService.getTableCodeItemByStandardCode("TJ0006");
    }

    /**
     * 给检验项目添加检验标本
     *
     * @param list
     * @return
     */
    @Override
    public String addSampleBatch(List<CpoeBasicDictInspecSample> list) {
        Map<String, Object> map = new HashMap<>();
        if (!list.isEmpty()) {
            inspecSampleMapper.insertBatch(list);
        }
        map.put("code", "0");
        return JSON.toJSONString(map);
    }

    /**
     * 删除检验项目标本
     *
     * @param list
     * @return
     */
    @Override
    public String delSample(List<CpoeBasicDictInspecSample> list) {
        Map<String, Object> map = new HashMap<>();
        if (!list.isEmpty()) {
            inspecSampleMapper.deleteBatch(list);
        }
        map.put("code", "0");
        return JSON.toJSONString(map);
    }

    /**
     * 保存申请单
     *
     * @param cpoeDictApplyNew
     * @return
     */
    @CacheEvict(key = "'emr::getAllApplyItemDetail[Empty]'")
    @Override
    public String saveApplyItem(CpoeDictApplyNew cpoeDictApplyNew) {
        if (cpoeDictApplyNew.getApplyId() == null) { //新增
            cpoeDictApplyNew.setApplyId(applyNewMapper.getSquence());
            applyNewMapper.insert(cpoeDictApplyNew);

            CpoeAppCaRelVst cpoeAppCaRelVst = new CpoeAppCaRelVst();
            cpoeAppCaRelVst.setApplyId(cpoeDictApplyNew.getApplyId());
            if (cpoeDictApplyNew.getCatalogId() == null) {
                cpoeAppCaRelVst.setExecOrgCode(cpoeDictApplyNew.getExecOrgCode());
//                stringRedisTemplate.delete("emr_AllSampleDetail");
            } else {
                cpoeAppCaRelVst.setCatalogId(cpoeDictApplyNew.getCatalogId());
            }
            cpoeAppCaRelVstMapper.insertSelective(cpoeAppCaRelVst);
        } else {
            applyNewMapper.update(cpoeDictApplyNew);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        return JSON.toJSONString(map);
    }

    /**
     * 删除申请单对应检验项目
     *
     * @param dictApplyInspRelNew
     * @return
     */
    @Override
    public String delApplyItemInsp(CpoeDictApplyInspRelNew dictApplyInspRelNew) {
        Map<String, Object> map = new HashMap<>();
        if (dictApplyInspRelNew != null) {
            dictApplyInspRelNewMapper.delete(dictApplyInspRelNew);
        }
        map.put("code", "0");
        return JSON.toJSONString(map);
    }

    /**
     * 添加申请单对应诊疗项目
     *
     * @param list
     * @return
     */
    @Override
    public String addApplyItemInsp(List<CpoeDictApplyInspRelNew> list) {
        Map<String, Object> map = new HashMap<>();
        if (!list.isEmpty()) {
            int seqence = dictApplyInspRelNewMapper.getSeqence();
            for (CpoeDictApplyInspRelNew dictApplyInspRelNew : list) {
                dictApplyInspRelNew.setDictInspecSampleId(seqence++);
            }
            dictApplyInspRelNewMapper.insertBatch(list);
        }
        map.put("code", "0");
        return JSON.toJSONString(map);
    }

    /**
     * 删除申请单
     *
     * @param applyId
     * @return
     */
    @CacheEvict(key = "'emr::getAllApplyItemDetail[Empty]'")
    @Override
    public String delApplyItem(Integer applyId) {
        cpoeAppCaRelVstMapper.delByApplyId(applyId);
        applyNewMapper.delete(applyId);
        dictApplyInspRelNewMapper.delByApplyId(applyId);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        return JSON.toJSONString(map);
    }

    /**
     * 绑定申请单
     *
     * @param list
     * @return
     */
    @Override
    public String bindApplyItem(List<CpoeAppCaRelVst> list) {
        if (!list.isEmpty()) {
            for (CpoeAppCaRelVst cpoeAppCaRelVst : list) {
                if (cpoeAppCaRelVst.getCatalogId() != null) {
                    cpoeAppCaRelVst.setExecOrgCode(null);
                }
            }
            cpoeAppCaRelVstMapper.insertBatch(list);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        return JSON.toJSONString(map);
    }

    /**
     * 获取所有申请单
     *
     * @return
     */
    @Cacheable
    @Override
    public String getAllApplyItemDetail() {
        List<CpoeDictApplyNew> list = applyNewMapper.selectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("data", list);
        return JSON.toJSONString(map);
    }
}
